package us.ohec.validators {
  import mx.validators.NumberValidator;
  import mx.validators.ValidationResult;
  
  import us.ohec.PostcodeData;

  public class PostcodeValidator extends NumberValidator {
    private var results:Array = [];

    public var invalidPostcode:String = "Please insert a valid Australian Postcode.";

    public var strong:Boolean = true;

    public function PostcodeValidator() {
      super();

      this.allowNegative = false;
      this.precision = 0;
      this.minValue = 200;
      this.maxValue = 9729;

      this.lowerThanMinError = invalidPostcode;
      this.exceedsMaxError = invalidPostcode;
      this.negativeError = invalidPostcode;
    }

    override protected function doValidation(value:Object): Array {

      var val:String = value.toString();

      results = super.doValidation(value);

      if (results.length > 0) {
        return results;
      }

      if (val == "" || val == null) {
        return results;
      }

      if (this.strong == true) {
        if (PostcodeData.suburbsByPostcode(val).length == 0) {
          results.push(new ValidationResult(true, null, "invalidPostcode", "Postcode does not exist."));
        }
      }

      return results;
    }
  }
}